package n2386;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main2 {

	static int a[][]=new int[101][101];
	
	static int father[]=new int[10001];
	
	static int rank[]=new int[10001];
	
	static int N,M;
	static void init()
	{
		for(int i=0;i<N*M;i++){
			father[i]=i;
			rank[i]=1;
		}
	}
	
	static int find(int x)
	{
		if(father[x]!=x)father[x]=find(father[x]);
		return father[x];
	}
	
	static int count;
	
	static boolean union(int x,int y)
	{
		x=find(x);
		y=find(y);
		if(x==y)return false;
		if(rank[x]<rank[y])
		{
			father[x]=y;
			rank[y]+=rank[x];
		}else{
			father[y]=x;
			rank[x]+=rank[y];
		}
		return true;
	}
	
	
	static int solve()
	{
		init();
		for(int i=0;i<N;i++)
			for(int j=0;j<M;j++)
			{
				if(a[i][j]==1)
				{
					count++;
					if(j>0&&a[i][j-1]==1&&union(i*M+j,i*M+j-1))
					{
						//左边
						count--;
					}
					if(i>0&&j>0&&a[i-1][j-1]==1&&union(i*M+j,(i-1)*M+j-1))
					{
						//左上
						count--;
					}
					if(i>0&&a[i-1][j]==1&&union(i*M+j,(i-1)*M+j))
					{
						//上
						count--;
					}
					if(i>0&&j+1<M&&a[i-1][j+1]==1&&union(i*M+j,(i-1)*M+j+1))
					{
						//右上
						count--;
					}
				}
				//System.out.println(i+","+j+":"+count);
			}
		
		return count;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args)  throws Exception{
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer(bf.readLine());
		N=Integer.parseInt(st.nextToken());
		M=Integer.parseInt(st.nextToken());
		for(int i=0;i<N;i++)
		{
			char c[]=bf.readLine().toCharArray();
			for(int j=0;j<M;j++)
			{
				if(c[j]=='W')a[i][j]=1;
				else a[i][j]=0;
			}
		}
		System.out.println(solve());
	}

}
